<template>
  <!-- 人员修改 -->
  <el-dialog :modal-append-to-body="false" :close-on-click-modal="false" destroy-on-close title="人员修改"
    :visible.sync="changeManVisible" width="500px" @close="changeManCancelFunc">
    <el-form ref="form" :model="changeManForm" label-width="100px" v-loading="changeLoading" label-position="left">
      <el-form-item label="职位">
        <el-select filterable disabled @change="changeManTypeFunc" class="change-man-input" v-model="changeManForm.type"
          placeholder="选择">
          <el-option v-for="item in changeManTypeList" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="客户经理" v-if="changeManForm.type == 1">
        <el-select filterable class="change-man-input" v-model="changeManForm.admin_id" placeholder="选择">
          <el-option v-for="item in changManToManOption" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设计师" v-if="changeManForm.type == 2">
        <el-select filterable class="change-man-input" v-model="changeManForm.admin_id" placeholder="选择">
          <el-option v-for="item in changManToManOption" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="造价员" v-if="changeManForm.type == 3">
        <el-select filterable class="change-man-input" v-model="changeManForm.admin_id" placeholder="选择">
          <el-option v-for="item in changManToManOption" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="施工工程经理" v-if="changeManForm.type == 4 || changeManForm.type == 7">
        <el-select filterable class="change-man-input" v-model="changeManForm.admin_id" placeholder="选择"
          @change="changeManEngineerFunc">
          <el-option v-for="item in changManToManOption" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工程监理" v-if="changeManForm.type == 7">
        <el-select filterable class="change-man-input" v-model="changeManForm.engineering_supervision_id"
          placeholder="选择">
          <el-option v-for="item in jLmanOption" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="勘场工程经理" v-if="changeManForm.type == 5">
        <el-select filterable class="change-man-input" v-model="changeManForm.admin_id" placeholder="选择">
          <el-option v-for="item in changManToManOption" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="changeManSubmit" :loading="changeLoading" :disabled="submitDisabled">提
        交</el-button>
      <el-button @click="changeManCancelFunc">关闭</el-button>
    </span>
  </el-dialog>
</template>
<script>
import adminApi from "@/api/adminApi";
import commonApi from "@/api/common";
export default {
  name: "",
  props: {
    // 弹窗显示
    changeDialogVisible: {
      type: Boolean,
      default: false,
      required: false,
    },
    // 修改人员类型（changeManTypeList.value）
    type: {
      type: Number,
      default: 1,
      required: false,
    },
    // 项目id
    projectId: {
      default: '',
      required: false,
    },
  },
  data() {
    return {
      changeLoading: false,
      jLmanOption: [],//监理列表
      // 修改人员
      changManToManOption: [],//人员列表
      changeManProjectObj: "",//项目基本数据
      changeManForm: {
        type: '',
        admin_id: '',
        engineering_supervision_id: ''
      },
      changeManVisible: false,
      changeManTypeList: [{
        label: '客户经理',
        value: 1,
      }, {
        label: '设计师',
        value: 2,
      }, {
        label: '造价员',
        value: 3,
      }, {
        label: '勘场工程经理',
        value: 4,
      }, {
        label: '施工工程经理',
        value: 7,
      }, {
        label: '勘场工程经理',
        value: 5,
      }
      ],
      admin_id: "",
      engineering_supervision_id: ""
    };
  },
  watch: {
    changeDialogVisible(value) {
      this.changeManVisible = value;
      this.changeManForm.type = this.type;
      if (!value) return;
      this.$nextTick(() => {
        this.selectProjectDetails(this.projectId);
      })
    },
  },
  computed: {
    submitDisabled() {
      if (this.changeManForm.type == 7) {
        return this.admin_id == this.changeManForm.admin_id && this.changeManForm.engineering_supervision_id == this.engineering_supervision_id;
      } else {
        return this.admin_id == this.changeManForm.admin_id;
      }
    },
  },
  mounted() { },
  methods: {
    // 施工工程经理选择
    changeManEngineerFunc(e) {
      if (this.changeManForm.type != 7 || !e) return;
      let obj = this.changManToManOption.find(item => {
        return item.id == e;
      })
      this.changeManForm.engineering_supervision_id = obj.engineering_supervision_id || '';
    },

    changeManCancelFunc() {
      this.$emit('changeManCancelFunc', true);
    },
    // 获取项目基本信息
    selectProjectDetails(project_id) {
      if (!this.projectId) return;
      commonApi.selectProjectDetails({
        project_id
      }).then(res => {
        console.log(res, "获取项目基本信息")
        if (res.code == 200) {
          this.engineering_supervision_id = res.data.engineering_supervision_id ? JSON.parse(JSON.stringify(res.data.engineering_supervision_id)) : '';
          this.changeManForm.engineering_supervision_id = res.data.engineering_supervision_id ? JSON.parse(JSON.stringify(res.data.engineering_supervision_id)) : '';
          res.data.project_city = [res.data.province_id, res.data.city_id, res.data.county_id];
          this.changeManTypeFunc(res.data);
        }
      })
    },
    // 修改人员类型选择
    changeManTypeFunc(projectObj) {
      let { type } = this.changeManForm;
      console.log(projectObj, "项目数据")
      switch (type) {
        case 1:
          this.changManToManList(2, projectObj.account_manager_id || '');//客户经理
          break;
        case 2:
          this.changManToManList(3, projectObj.designer_id || '');//设计师
          break;
        case 3:
          this.changManToManList(5, projectObj.cost_estimator_id || '');//造价员
          break;
        case 4:
          this.changManToManList(7, projectObj.engineering_manager_id || '');//勘场工程经理
        case 7:
          this.changManToManList(7, projectObj.engineering_manager_id || '');//施工工程经理
          this.changManToManList(7, projectObj.engineering_manager_id || '', type);//工程监理
          break;
        case 5:
          this.changManToManList(7, projectObj.site_surveyors_id || '');//勘场工程经理
          break;
        default:
      }
      this.changeManProjectObj = projectObj;
    },
    // 修改人员（请求对应的职位列表）  type存在请求工程监理列表
    changManToManList(position_id_arr, admin_id, type) {
      this.changeLoading = true;
      adminApi.getAdminList({
        page: 1,
        num: 999,
        identity: type ? 1 : position_id_arr == 7 ? 2 : 1,
        position_id_arr: type ? [8] : [position_id_arr]
      }).then(res => {
        console.log(res, "修改人员（请求对应的职位列表）")
        this.changeLoading = false;
        if (res.code == 200) {
          if (type) {
            this.jLmanOption = res.data.list;
            return;
          }

          this.changManToManOption = res.data.list;
          this.$nextTick(() => {
            this.admin_id = JSON.parse(JSON.stringify(admin_id));
            this.changeManForm.admin_id = JSON.parse(JSON.stringify(admin_id));
          })
        }
      })
    },
    // 修改人员确认
    changeManSubmit() {
      let changeManForm = this.changeManForm;
      if (!changeManForm.type) {
        this.$message({
          type: 'warning',
          message: "请先选择需要分配的职位"
        })
        return;
      }
      this.updProjectLeader(changeManForm)
    },
    // 修改人员请求
    updProjectLeader(obj) {
      obj = JSON.parse(JSON.stringify(obj))
      this.changeLoading = true;
      obj.project_id_arr = [this.projectId];//项目id
      obj.type = obj.type == 7 ? 4 : obj.type;
      commonApi.updProjectLeader(obj).then(res => {
        console.log(res, "修改人员")
        this.changeLoading = false;
        if (res.code == 200) {
          this.$emit('changeManSuccessFunc', true);
        }
        this.$message({
          type: res.code == 200 ? 'success' : 'warning',
          message: res.message
        })
      })
    },
  },
};
</script>
<style lang="scss" scoped>
@import "./index.scss";
</style>